package org.zzh.service.cargo.extCproduct;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zzh.dao.common.PageBean;
import org.zzh.dao.mapper.CoExtCproductMapper;
import org.zzh.domain.cargo.extCproduct.CoExtCproduct;

import java.util.List;
import java.util.UUID;

@Service
public class CoExtCproductServiceImpl implements CoExtCproductService {

    @Autowired
    private CoExtCproductMapper coExtCproductMapper;

    @Override
    public PageBean<CoExtCproduct> list(String contractId, String contractProductId, int page, int size) {
        PageHelper.startPage(page, size);
        List<CoExtCproduct> list = coExtCproductMapper.list(contractId, contractProductId);
        PageInfo<CoExtCproduct> coExtCproductPageInfo = new PageInfo<>(list);
        PageBean<CoExtCproduct> coExtCproductPageBean = new PageBean<>();
        coExtCproductPageBean.setData(coExtCproductPageInfo.getList());
        coExtCproductPageBean.setPage(page);
        coExtCproductPageBean.setPageSize(size);
        coExtCproductPageBean.setTotalCount((int) coExtCproductPageInfo.getTotal());
        coExtCproductPageBean.setTotalPage(coExtCproductPageInfo.getPages());
        return coExtCproductPageBean;
    }

    @Override
    public CoExtCproduct findById(String id) {
        return coExtCproductMapper.findById(id);
    }

    @Override
    public void delete(String id) {
        coExtCproductMapper.deleteById(id);
    }

    @Override
    public void edit(CoExtCproduct coExtCproduct) {
        if (coExtCproduct.getId()!=null&&!coExtCproduct.getId().isEmpty()){
            coExtCproductMapper.update(coExtCproduct);
            return;
        }
        coExtCproduct.setId(UUID.randomUUID().toString());
        coExtCproductMapper.add(coExtCproduct);
        return;
    }

}
